package org.example.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.dto.BuildingDto;
import org.example.dto.DepartmentDto;
import org.example.service.BuildingService;
import org.example.service.TestService;
import org.example.utils.JsonUtils;
import org.example.vo.BuildingVo;
import org.example.vo.DepartmentVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Rongchang Wu
 * @version 1.0
 * @description: TODO
 * @date 2021/12/9 15:19
 */
@RestController
public class BuildingController {
    @Resource
    BuildingService service;

    @GetMapping("/getBuildingByName")
    public void getBuildingByName(HttpServletResponse resp, String buildingName) throws IOException {
        BuildingDto building = service.queryBuildingByName(buildingName);
        //以上service根据系别名查找，返回对应的系
        List<BuildingDto> buildingList = new ArrayList<>();
        buildingList.add(building);

        JsonUtils.customJson(resp, buildingList, buildingList.size());
    }

    @GetMapping("/getBuildingList")
    public void getBuildingList(HttpServletResponse resp, Integer page, Integer limit) throws IOException {
        Page<Object> obj = PageHelper.startPage(page, limit);
        List<BuildingDto> buildingList = service.queryAllBuilding();
        Integer count = service.countBuilding();
        //以上调用service查找所有系别，赋值给buildingList；查找系别数量，赋值给count

        JsonUtils.customJson(resp, buildingList, count);
    }

    @GetMapping(value = "/deleteBuilding", produces = "text/plain;charset=utf-8")
    public String deleteBuilding(String buildingId) {
        String[] buildingIds = buildingId.split(",");
        Integer status = 0;
        for (int i = 0; i < buildingIds.length; i++) {
            status += service.deleteBuilding(Integer.parseInt(buildingIds[i]));
        }
        //以上调用service方法删除系别

        return status > 0 ? "success" : "error";
    }

    @GetMapping(value = "/addBuilding", produces = "text/plain;charset=utf-8")
    public String addBuilding(BuildingVo building) {
        Integer status = service.addBuilding(building);
        //以上调用service方法添加系别

        return status > 0 ? "success" : "error";
    }

    @GetMapping("/getBuildingById")
    public BuildingDto getBuildingById(Integer buildingId) {
        BuildingDto building = service.queryBuildingById(buildingId);
        //以上service根据系系号查找，返回对应的系

        return building;
    }

    @GetMapping("/editBuilding")
    public String editBuilding(BuildingVo building) {
        Integer status = service.updateBuilding(building);
        //以上调用service方法修改系别

        return status > 0 ? "success" : "error";
    }
}
